package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.sysconfig.SysConfigDO;
import com.example.demo.exception.OtherException;
import com.example.demo.mapper.sysconfig.SysConfigMapper;
import com.example.demo.service.ISysConfigService;
import com.example.demo.util.RedisUtil;
import com.example.demo.dto.sysconfig.SysConfigPageReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfigDO> implements ISysConfigService {

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Resource
    private RedisUtil redisUtil;

    private final static String SYS_CONFIG_KEY = "sys_config_key";

    @Override
    public PageRes<SysConfigDO> list(SysConfigPageReqVO sysConfigPageReqVO) {
        return sysConfigMapper.selectPage(sysConfigPageReqVO);
    }

    @Override
    public List<SysConfigDO> listByConfigKey(String... configKey) {
        return sysConfigMapper.selectList(new QueryWrapper<SysConfigDO>().in("config_key",configKey));
    }

    @Override
    public int addSysConfig(SysConfigDO sysConfigDO) {
        validSysConfigKeyExist(sysConfigDO.getConfigKey(), null);
        int result = sysConfigMapper.insert(sysConfigDO);
        refreshCache(SYS_CONFIG_KEY);
        return result;
    }

    @Override
    public int deleteSysConfig(SysConfigDO sysConfigDO) {
        validSysConfigExist(sysConfigDO.getId());
        int result = sysConfigMapper.deleteById(sysConfigDO.getId());
        refreshCache(SYS_CONFIG_KEY);
        return result;
    }

    @Override
    public int updateSysConfig(SysConfigDO sysConfigDO) {
        validSysConfigExist(sysConfigDO.getId());
        validSysConfigKeyExist(sysConfigDO.getConfigKey(), sysConfigDO);
        int result = sysConfigMapper.updateById(sysConfigDO);
        refreshCache(SYS_CONFIG_KEY);
        return result;
    }

    @Override
    public SysConfigDO getSysConfig(SysConfigDO sysConfigDO) {
        validSysConfigExist(sysConfigDO.getId());
        return sysConfigMapper.selectById(sysConfigDO.getId());
    }

    /**
     * 刷新缓存
     * @param keyName
     */
    public void refreshCache(String keyName){
        // 获取系统配置列表
        List<SysConfigDO> list = sysConfigMapper.selectList(null);
        redisUtil.set(keyName, list);
    }

    private void validSysConfigKeyExist(String configKey, SysConfigDO oldInfo) {
        QueryWrapper<SysConfigDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("config_key", configKey);
        if (oldInfo != null) {
            queryWrapper.ne("id", oldInfo.getId());
        }
        SysConfigDO exist = sysConfigMapper.selectOne(queryWrapper);
        if (exist != null) {
            throw new OtherException("配置项已存在");
        }
    }

    private void validSysConfigExist(Long id) {
        SysConfigDO exist = sysConfigMapper.selectById(id);
        if (exist == null) {
            throw new OtherException("配置不存在");
        }
    }
}
